﻿using System;
using System.Linq;
using System.Net;
using Grundy.Message;
using Grundy.Message.Administration;
using log4net;

namespace Grundy.Licensing.Server.Command
{
    public class GetInstalledLicensesCommand : ICommand
    {
        ILog _logger = log4net.LogManager.GetLogger(typeof(GetInstalledLicensesCommand));

        public bool CanProcess(Request request)
        {
            return request is GetInstalledLicensesRequest;
        }

        public Response Process(Request request, EndPoint requestingEndPoint, Guid customerId, string customerName)
        {
            return Process((GetInstalledLicensesRequest)request);

        }

        private Response Process(GetInstalledLicensesRequest request)
        {
            var application = Application.Instance;
            var licensesData = application.GetLicensesData();
            var results = licensesData.Select(i => new Message.InstalledLicenseInformation
                                                       {
                                                           License = i.CustomerLicenseEntitlement.License,
                                                           Customer = i.CustomerLicenseEntitlement.CustomerName ,
                                                           Product = i.CustomerLicenseEntitlement.ProductName,
                                                           Exhausted = i.GetExhaustedLicenseCount()
                                                       }).ToList();

            return new InstalledLicensesResponse { LicensesData = results };
        }
    }
}